package com.flying.carclub.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;

/**
 * Created by zhuxiaocheng on 2018/1/23.
 */

public class TitleBar extends RelativeLayout implements View.OnClickListener {
    private Context context;
    private static final int DEFAULT_RES_ID = -1;
    private TextView mTitleTv;// 标题栏
    private View mLeftBtn;
    private TextView mRightTv;
    private String nRightBtnText1, nRightBtnText2, nLeftBtnText, nTitleText;
    private int nRightBtnIcon1 = DEFAULT_RES_ID, nRightBtnIcon2 = DEFAULT_RES_ID;
    private int nRightBtnTextAppearance1 = DEFAULT_RES_ID, nRightBtnTextAppearance2 = DEFAULT_RES_ID;

    private boolean bShowLeftBtn, bShowRightBtn1, bShowRightBtn2;
    private OnTitleClickListener onTitleClickListener;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initFromAttr(context, attrs);
    }


    private void initFromAttr(Context context, AttributeSet attrs) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.widget_title_bar, this);
        mTitleTv = findViewById(R.id.tv_title);
        mLeftBtn = findViewById(R.id.ibtn_back);
        mRightTv = findViewById(R.id.tv_right);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DxlTitleView);

            nTitleText = a.getString(R.styleable.DxlTitleView_title_text);

            bShowLeftBtn = a.getBoolean(R.styleable.DxlTitleView_title_show_left_btn, true);
            nRightBtnText1 = a.getString(R.styleable.DxlTitleView_title_btn_right_text);
            a.recycle();

        } else {

        }
        initView(context);
    }

    private void initView(Context context) {
        mLeftBtn.setVisibility(bShowLeftBtn? VISIBLE: GONE);

        mTitleTv.setText(nTitleText);
        if (!TextUtils.isEmpty(nRightBtnText1)){
            mRightTv.setVisibility(VISIBLE);
            mRightTv.setText(nRightBtnText1);
            mRightTv.setOnClickListener(this);
        }else {
            mRightTv.setVisibility(GONE);
        }
        mLeftBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ibtn_back:
                sendKeyBackEvent();
                break;
            case R.id.tv_right:
                if (onTitleClickListener != null){
                    onTitleClickListener.onRightClick();
                }
                break;
        }

    }
    public void setTitle(String title){
        nTitleText = title;
        mTitleTv.setText(nTitleText);
    }
    private void sendKeyBackEvent() {
        final Context context = getContext();
        if (context instanceof BaseActivity) {
            KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
            ((BaseActivity) context).onKeyDown(KeyEvent.KEYCODE_BACK, keyEvent);
        }
    }
    /**
     * @param onTitleClickListener the onTitleClickListener to set
     */
    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
        this.onTitleClickListener = onTitleClickListener;
    }

    public interface OnTitleClickListener {

        /**
         * 左侧按钮点击处理，返回值：true（已消费，不抛出），false（向上抛出）
         *
         * @return
         */
        boolean onLeftClick();

        void onRightClick();

    }
}
